package top.buyibuyi.course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.pojo.Admin;
import top.buyibuyi.course.pojo.Course;
import top.buyibuyi.course.pojo.PageBean;
import top.buyibuyi.course.service.SelectCourseService;

import java.util.List;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class SelectCourseController {
    @Autowired
    private SelectCourseService selectCourseService;

    @PostMapping("/selectCourse/{page}/{limit}")
    public R selectCourse(@PathVariable int page, @PathVariable int limit,
                          @RequestBody Course course){

        PageBean<Course> coursePageBean = new PageBean<>();
        System.out.println("页面==》"+page+"页面大小==》"+limit+"条件==》"+course);
        //对pageBean的七个属性赋值
        //页大小
        coursePageBean.setLimit(limit);
        //总数居条数
        int count = selectCourseService.selectCount(course);
        System.out.println("kkkkkkk"+count);
        coursePageBean.setTotal(count);
        //设置当前页
        coursePageBean.setPage(page);
        //是否有上一页
        if(page>1){
            coursePageBean.setHasPrevPage(true);
        }else {
            coursePageBean.setHasPrevPage(false);
        }
        //是否有下一页
        if(coursePageBean.getPage() >= coursePageBean.getTotalPage()){
            coursePageBean.setHasNextPage(false);
        }else {
            coursePageBean.setHasNextPage(true);
        }
        //设置数据
        try {
            int startPage = 0;
            startPage = (page-1)*limit;
            System.out.println(startPage);
            System.out.println(course.getCname());
            List<Course> courses = selectCourseService.selectCourse(startPage, limit, course.getCname());
            coursePageBean.setPageRecode(courses);
            return R.ok().data("data",coursePageBean);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("....");
        }

    }

    @GetMapping("/selectCourseById/{id}")
    public R selectCourseById(@PathVariable int id){
        try {
            Course course = selectCourseService.selectCourseById(id);
            return R.ok().data("data",course);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
